/**
 * 时间工具函数
 * 计算相对时间：几秒前、几分钟前、几小时前、几天前等
 */

/**
 * 计算相对时间
 * @param dateTime 时间字符串或时间戳
 * @returns 相对时间字符串
 */
export function getRelativeTime(dateTime: string | number | Date): string {
  if (!dateTime) return ''
  
  const now = new Date().getTime()
  const targetTime = new Date(dateTime).getTime()
  
  // 如果时间无效，返回原始值
  if (isNaN(targetTime)) {
    return typeof dateTime === 'string' ? dateTime : ''
  }
  
  const diff = now - targetTime
  
  // 如果是未来时间，直接返回格式化的日期
  if (diff < 0) {
    return formatDateTime(new Date(targetTime))
  }
  
  const seconds = Math.floor(diff / 1000)
  const minutes = Math.floor(seconds / 60)
  const hours = Math.floor(minutes / 60)
  const days = Math.floor(hours / 24)
  const months = Math.floor(days / 30)
  const years = Math.floor(days / 365)
  
  if (years > 0) {
    return `${years}年前`
  } else if (months > 0) {
    return `${months}个月前`
  } else if (days > 0) {
    return `${days}天前`
  } else if (hours > 0) {
    return `${hours}小时前`
  } else if (minutes > 0) {
    return `${minutes}分钟前`
  } else if (seconds > 30) {
    return `${seconds}秒前`
  } else {
    return '刚刚'
  }
}

/**
 * 格式化日期时间
 * @param date Date对象
 * @returns 格式化的日期时间字符串
 */
function formatDateTime(date: Date): string {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  return `${year}-${month}-${day} ${hours}:${minutes}`
}

/**
 * 智能时间显示
 * 根据时间距离现在的长短，选择合适的显示方式
 * @param dateTime 时间字符串或时间戳
 * @returns 智能格式化的时间字符串
 */
export function getSmartTime(dateTime: string | number | Date): string {
  if (!dateTime) return ''
  
  const now = new Date()
  const targetDate = new Date(dateTime)
  
  // 如果时间无效，返回原始值
  if (isNaN(targetDate.getTime())) {
    return typeof dateTime === 'string' ? dateTime : ''
  }
  
  const diff = now.getTime() - targetDate.getTime()
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  
  // 今天：显示相对时间
  if (days === 0) {
    return getRelativeTime(dateTime)
  }
  
  // 昨天
  if (days === 1) {
    const hours = String(targetDate.getHours()).padStart(2, '0')
    const minutes = String(targetDate.getMinutes()).padStart(2, '0')
    return `昨天 ${hours}:${minutes}`
  }
  
  // 前天
  if (days === 2) {
    const hours = String(targetDate.getHours()).padStart(2, '0')
    const minutes = String(targetDate.getMinutes()).padStart(2, '0')
    return `前天 ${hours}:${minutes}`
  }
  
  // 本年内：显示月-日 时:分
  if (now.getFullYear() === targetDate.getFullYear()) {
    const month = String(targetDate.getMonth() + 1).padStart(2, '0')
    const day = String(targetDate.getDate()).padStart(2, '0')
    const hours = String(targetDate.getHours()).padStart(2, '0')
    const minutes = String(targetDate.getMinutes()).padStart(2, '0')
    return `${month}-${day} ${hours}:${minutes}`
  }
  
  // 跨年：显示完整日期
  return formatDateTime(targetDate)
}